package com.yykj.hospital.server.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yykj.common.vo.PageData;
import com.yykj.common.vo.ResultVO;
import com.yykj.hospital.server.dto.DoctorPasswordDTO;
import com.yykj.hospital.server.dto.PayPasswordCheckForm;
import com.yykj.hospital.server.dto.PayPasswordCodeForm;
import com.yykj.hospital.server.dto.PayPasswordSetForm;
import com.yykj.hospital.server.dto.account.DoctorBalanceLogDTO;
import com.yykj.hospital.server.dto.bank.DoctorCashFeeTypeDTO;
import com.yykj.hospital.server.enums.IncomeExpendType;
import com.yykj.hospital.server.form.CashApplyForm;
import com.yykj.hospital.server.form.account.DoctorAccountSearchForm;
import com.yykj.hospital.server.service.DoctorPasswordService;
import com.yykj.hospital.server.service.account.DoctorAccountService;
import com.yykj.hospital.server.service.bank.BankService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("/api/doctor/balance")
@Api(tags = {"医生端APP 余额相关接口"})
@SuppressWarnings("unchecked")
public class DoctorClientBalanceController {

    @ApiOperation("检查是否设置过支付密码")
    @GetMapping("/check/pay/password/{doctorId}")
    public ResultVO<DoctorPasswordDTO> checkPayPasswordSet(@PathVariable String doctorId){
        return ResultVO.success(doctorPasswordService.checkPayPassword(doctorId));
    }

    @ApiOperation("支付密码验证码 -- 发送")
    @PostMapping("/pay/password/send")
    @ApiOperationSupport(ignoreParameters = {"code"})
    public ResultVO sendPayCheckCode(@RequestBody PayPasswordCodeForm sendForm){
        doctorPasswordService.sendPayCheckCode(sendForm);
        return ResultVO.success();
    }

    @ApiOperation("支付密码验证码--验证")
    @PostMapping("/pay/password/check")
    @ApiOperationSupport(ignoreParameters = {"doctorId"})
    public ResultVO checkPayCheckCode(@RequestBody PayPasswordCodeForm sendForm){
        return ResultVO.success(doctorPasswordService.checkPayCheckCode(sendForm));
    }

    @ApiOperation("支付密码--设置")
    @PostMapping("/pay/password/setting")
    @ApiOperationSupport(ignoreParameters = {"oldPassword"})
    public ResultVO payPasswordSetting( @RequestBody PayPasswordSetForm form){
        doctorPasswordService.payPasswordSetting(form);
        return ResultVO.success();
    }

    @ApiOperation("支付密码--验证支付密码是否正确")
    @PostMapping("/check/pay/password")
    @ApiOperationSupport(ignoreParameters = {"oldPassword"})
    public ResultVO checkPayPasswordRight( @RequestBody PayPasswordCheckForm form){
        return ResultVO.success(doctorPasswordService.checkPayPasswordRight(form.getDoctorId(),form.getPassword()));
    }

    @ApiOperation("支付密码--通过旧密码修改")
    @PostMapping("/pay/password/resetting")
    public ResultVO payPasswordResetting(@Valid @RequestBody PayPasswordSetForm form){
        doctorPasswordService.payPasswordResetting(form);
        return ResultVO.success();
    }


    @ApiOperation("提现申请--发起")
    @PostMapping("/cash/apply")
    public ResultVO createCashApply(@RequestBody CashApplyForm form){
        bankService.cashToBankApply(form);
        return ResultVO.success();
    }

    @ApiOperation("提现申请--获取提现比率或者固定额")
    @PostMapping("/cash/fee/type/{doctorId}")
    public ResultVO<DoctorCashFeeTypeDTO> cashFeeType(@PathVariable String doctorId){

        return ResultVO.success(bankService.cashFeeType(doctorId));
    }

    @ApiOperation("提现和收益明细")
    @PostMapping("/log")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生ID",required = true),
            @ApiImplicitParam(name = "startDate", value = "开始日期"),
            @ApiImplicitParam(name = "endDate", value = "结束日期"),
            @ApiImplicitParam(name = "incomeExpend", value = "income:收益，expend:支出"),
            @ApiImplicitParam(name = "currentPage", value = "当前页，默认1"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小:默认10")
    })
    public ResultVO<PageData<DoctorBalanceLogDTO>> listDoctorBalanceLog(@RequestParam(name = "doctorId") String doctorId,
                                                                        @RequestParam(name = "startDate", required = false) String startDate,
                                                                        @RequestParam(name = "endDate", required = false) String endDate,
                                                                        @RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                        @RequestParam(name = "incomeExpend") IncomeExpendType incomeExpend
                                                                        ) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate start = null;
        if (!StringUtils.isEmpty(startDate)) {
            start = LocalDate.parse(startDate, dateTimeFormatter);
        }
        LocalDate end = null;
        if (!StringUtils.isEmpty(endDate)) {
            end = LocalDate.parse(endDate, dateTimeFormatter);
        }

        DoctorAccountSearchForm form = DoctorAccountSearchForm.builder()
                .doctorId(doctorId)
                .incomeExpend(incomeExpend)
                .startDate(start)
                .endDate(end)
                .currentPage(currentPage)
                .pageSize(pageSize)
                .build();

        return ResultVO.success(doctorAccountService.listDoctorBalanceLog(form));
    }


    private DoctorPasswordService doctorPasswordService;

    private BankService bankService;

    private DoctorAccountService doctorAccountService;


    @Autowired
    public void setDoctorPasswordService(DoctorPasswordService doctorPasswordService) {
        this.doctorPasswordService = doctorPasswordService;
    }
    @Autowired
    public void setBankService(BankService bankService) {
        this.bankService = bankService;
    }
    @Autowired
    public void setDoctorAccountService(DoctorAccountService doctorAccountService) {
        this.doctorAccountService = doctorAccountService;
    }
}
